﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace UBCon.Navigation
{
    internal class UIElementAdorner : Adorner
    {
        TranslateTransform translate = new TranslateTransform();

        internal UIElementAdorner(UIElement adornedElement)
            : base(adornedElement)
        {
            Content.RenderTransform = translate;
            Content.IsHitTestVisible = false;
        }

        protected override Visual GetVisualChild(int index)
        {
            return Content;
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return 1;
            }
        }

        internal double xOffset = 0, yOffset = 0;

        protected override Size MeasureOverride(Size constraint)
        {
            Size size = new Size(CapturedItem.DesiredSize.Width, CapturedItem.DesiredSize.Height);
            
            Content.Measure(size);
            
            return size;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            Content.Arrange(new Rect(finalSize));
            return finalSize;
        }

        internal void UpdateLocation(Point point, Orientation orientation) 
        {
            if (orientation == Orientation.Horizontal)
            {
                translate.X = point.X - xOffset;
                translate.Y = point.Y;
            }
            else
            {
                translate.Y = point.Y - yOffset;
            }
            InvalidateMeasure();
            
        }

        private UIElement capturedItem;
        
        internal UIElement CapturedItem 
        {
            get 
            {
                return capturedItem;
            }

            set 
            {
                capturedItem = value;
                
                if (value != null) 
                {
                    UIElement elt=null;
                    if (value is ICloneable)
                    {
                        elt = (value as ICloneable).Clone();
                    }
                    else
                    {
                        elt = NavigationHelper.XamlClone<UIElement>(value as UIElement);
                    }

                    if (elt != null)
                    {
                        elt.SetValue(Selector.IsSelectedProperty, false);
                        
                        elt.SetValue(TextBlock.FontWeightProperty, FontWeights.Light);

                        ControlTemplate template = capturedItem.GetValue(Control.TemplateProperty) as ControlTemplate;
                        
                        if (template != null)
                        {
                            elt.SetValue(Control.TemplateProperty, template);
                        }
                    }
                    
                    Content.Content = elt;
                    
                    value.Visibility = System.Windows.Visibility.Hidden;
                }
            }
        }

        internal ContentControl Content = new ContentControl();
    }
}
